package com.zhx.threadcoreknowledge.stopthread;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-09-29 09:08
 * 用途         ： 如何正确的停止阻塞线程
 */
public class RightWayStopThreadWithSleep implements Runnable{

    @Override
    public void run() {

    }

    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () ->{
            try {
                int num = 0;

                while (num <= 300 && !Thread.currentThread().isInterrupted()){
                    if(num % 100 == 0){
                        System.out.println(num +"可以被100整除");
                    }
                    num++;
                }
                System.out.println("结束");

                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(500);
        thread.interrupt();
    }
}
